<?php
/**
 * Kunena Component
 * @package Kunena.Site
 * @subpackage Controllers
 *
 * @copyright (C) 2008 - 2013 Kunena Team. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @link http://www.kunena.org
 **/
defined ( '_JEXEC' ) or die ();

/**
 * Kunena Topics Controller
 *
 * @since		2.0
 */
class KunenaControllerTopics extends KunenaController {

	function none() {
		$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_CONTROLLER_NO_TASK' ) );
		$this->redirectBack ();
	}

	function permdel() {
		if (! JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$message = '';
		$topics = KunenaForumTopicHelper::getTopics(array_keys(JRequest::getVar('topics', array ( 0 ), 'post', 'array')));
		if (!$topics) {
			$message = JText::_ ( 'COM_KUNENA_NO_TOPICS_SELECTED' );
		} else {
			foreach ( $topics as $topic ) {
				if ($topic->authorise('permdelete') && $topic->delete()) {
					// Activity integration
					$activity = KunenaFactory::getActivityIntegration();
					$activity->onAfterDeleteTopic($topic);
					$message = JText::_ ( 'COM_KUNENA_BULKMSG_DELETED' );
				} else {
					$this->app->enqueueMessage ( $topic->getError (), 'notice' );
				}
			}
		}
		if ($message) $this->app->enqueueMessage ( $message );
		$this->redirectBack ();
	}

	function delete() {
		if (! JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$message = '';
		$topics = KunenaForumTopicHelper::getTopics(array_keys(JRequest::getVar('topics', array ( 0 ), 'post', 'array')));
		if (!$topics) {
			$message = JText::_ ( 'COM_KUNENA_NO_TOPICS_SELECTED' );
		} else {
			foreach ( $topics as $topic ) {
				if ($topic->authorise('delete') && $topic->publish(KunenaForum::TOPIC_DELETED)) {
					$message = JText::_ ( 'COM_KUNENA_BULKMSG_DELETED' );
				} else {
					$this->app->enqueueMessage ( $topic->getError (), 'notice' );
				}
			}
		}
		if ($message) $this->app->enqueueMessage ( $message );
		$this->redirectBack ();
	}

	function restore() {
		if (! JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$message = '';
		$topics = KunenaForumTopicHelper::getTopics(array_keys(JRequest::getVar('topics', array ( 0 ), 'post', 'array')));
		if (!$topics) {
			$message = JText::_ ( 'COM_KUNENA_NO_TOPICS_SELECTED' );
		} else {
			foreach ( $topics as $topic ) {
				if ($topic->authorise('undelete') && $topic->publish(KunenaForum::PUBLISHED)) {
					$message = JText::_ ( 'COM_KUNENA_POST_SUCCESS_UNDELETE' );
				} else {
					$this->app->enqueueMessage ( $topic->getError (), 'notice' );
				}
			}
		}
		if ($message) $this->app->enqueueMessage ( $message );
		$this->redirectBack ();
	}

	public function approve() {
		if (! JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$message = '';
		$topics = KunenaForumTopicHelper::getTopics(array_keys(JRequest::getVar('topics', array ( 0 ), 'post', 'array')));
		if (!$topics) {
			$message = JText::_ ( 'COM_KUNENA_NO_TOPICS_SELECTED' );
		} else {
			foreach ( $topics as $topic ) {
				if ($topic->authorise('approve') && $topic->publish(KunenaForum::PUBLISHED)) {
					$message = JText::_ ( 'COM_KUNENA_MODERATE_APPROVE_SUCCESS' );
					$topic->sendNotification();
				} else {
					$this->app->enqueueMessage ( $topic->getError (), 'notice' );
				}
			}
		}
		if ($message) $this->app->enqueueMessage ( $message );
		$this->redirectBack ();
	}

	function move() {
		if (! JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$topics = KunenaForumTopicHelper::getTopics(array_keys(JRequest::getVar('topics', array ( 0 ), 'post', 'array')));
		if (!$topics) {
			$message = JText::_ ( 'COM_KUNENA_NO_TOPICS_SELECTED' );
		} else {
			$target = KunenaForumCategoryHelper::get(JRequest::getInt('target', 0));
			if (!$target->authorise('read')) {
				$this->app->enqueueMessage ( $target->getError(), 'error' );
			} else {
				foreach ( $topics as $topic ) {
					if ($topic->authorise('move') && $topic->move($target)) {
						$message = JText::_ ( 'COM_KUNENA_POST_SUCCESS_MOVE' );
					} else {
						$this->app->enqueueMessage ( $topic->getError (), 'notice' );
					}
				}
			}
		}
		if (!empty($message)) $this->app->enqueueMessage ( $message );
		$this->redirectBack ();
	}

	function unfavorite() {
		if (! JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$topics = KunenaForumTopicHelper::getTopics(array_keys(JRequest::getVar('topics', array ( 0 ), 'post', 'array')));
		if ( KunenaForumTopicHelper::favorite(array_keys($topics), 0) ) {
			$this->app->enqueueMessage ( JText::_('COM_KUNENA_USER_UNFAVORITE_YES') );
		} else {
			$this->app->enqueueMessage ( JText::_('COM_KUNENA_POST_NO_UNFAVORITED_TOPIC') );
		}
		$this->redirectBack ();
	}

	function unsubscribe() {
		if (! JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$topics = KunenaForumTopicHelper::getTopics(array_keys(JRequest::getVar('topics', array ( 0 ), 'post', 'array')));
		if ( KunenaForumTopicHelper::subscribe(array_keys($topics), 0) ) {
			$this->app->enqueueMessage ( JText::_('COM_KUNENA_USER_UNSUBSCRIBE_YES') );
		} else {
			$this->app->enqueueMessage ( JText::_('COM_KUNENA_POST_NO_UNSUBSCRIBED_TOPIC') );
		}
		$this->redirectBack ();
	}

	public function approve_posts() {
		if (!JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$success = 0;
		$messages = KunenaForumMessageHelper::getMessages(array_keys(JRequest::getVar('posts', array ( 0 ), 'post', 'array')));
		if (!$messages) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_NO_MESSAGES_SELECTED' ) );
		} else {
			foreach ( $messages as $message ) {
				if ($message->authorise('approve') && $message->publish(KunenaForum::PUBLISHED)) {
					$message->sendNotification();
					$success++;
				} else {
					$this->app->enqueueMessage ( $message->getError (), 'notice' );
				}
			}
		}
		if ($success) $this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_MODERATE_APPROVE_SUCCESS' ) );
		$this->redirectBack ();
	}

	public function delete_posts() {
		if (!JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$success = 0;
		$messages = KunenaForumMessageHelper::getMessages(array_keys(JRequest::getVar('posts', array ( 0 ), 'post', 'array')));
		if (!$messages) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_NO_MESSAGES_SELECTED' ) );
		} else {
			foreach ( $messages as $message ) {
				if ($message->authorise('delete') && $message->publish(KunenaForum::DELETED)) {
					$success++;
				} else {
					$this->app->enqueueMessage ( $message->getError (), 'notice' );
				}
			}
		}
		if ($success) $this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_POST_SUCCESS_DELETE' ) );
		$this->redirectBack ();
	}

	function restore_posts() {
		if (! JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$success = 0;
		$messages = KunenaForumMessageHelper::getMessages(array_keys(JRequest::getVar('posts', array ( 0 ), 'post', 'array')));
		if (!$messages) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_NO_MESSAGES_SELECTED' ) );
		} else {
			foreach ( $messages as $message ) {
				if ($message->authorise('undelete') && $message->publish(KunenaForum::PUBLISHED)) {
					$success++;
				} else {
					$this->app->enqueueMessage ( $message->getError (), 'notice' );
				}
			}
		}
		if ($success) $this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_POST_SUCCESS_UNDELETE' ) );
		$this->redirectBack ();
	}

	function permdel_posts() {
		if (! JSession::checkToken('post')) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
			$this->redirectBack ();
		}

		$success = 0;
		$messages = KunenaForumMessageHelper::getMessages(array_keys(JRequest::getVar('posts', array ( 0 ), 'post', 'array')));
		if (!$messages) {
			$this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_NO_MESSAGES_SELECTED' ) );
		} else {
			foreach ( $messages as $message ) {
				if ($message->authorise('permdelete') && $message->delete()) {
					$success++;
				} else {
					$this->app->enqueueMessage ( $message->getError (), 'notice' );
				}
			}
		}
		if ($success) $this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_BULKMSG_DELETED' ) );
		$this->redirectBack ();
	}
}